﻿using KTD.Web.BLL;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XXS.Common;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    public class NewsController : Controller
    {
        List<SelectListItem> desList = new List<SelectListItem>() {
            new SelectListItem(){Text="楼盘头条",Value="1"},
            new SelectListItem(){Text="吃瓜吐槽",Value="2"},
            new SelectListItem(){Text="销售技巧",Value="3"},
            new SelectListItem(){Text="政策解读",Value="4"}
        };
        // GET: News
        public ActionResult Index(NewsQD im)
        {
            im.PageSize = 10;
            var res = MF.NewsManager.GetListBy(im);
            return View(res);
        }

        [HttpPost]
        public ActionResult Create(NewsCUD im)
        {
            im.thumb= $"/Attachment/{Guid.NewGuid().ToString()}/{im.file.FileName}";
            var combinePath = Server.MapPath($"~{im.thumb }");

            Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

            im.file.SaveAs(combinePath);

            MF.NewsManager.Create(im);
            return this.JsonNet(new { succuess = true }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.CityList = MF.AreaManager.GetSelect();
            ViewBag.desList = desList;
            var model = new NewsCUD();
            return View(model);
        }



        [HttpGet]
        public ActionResult Update(int Id)
        {
            ViewBag.CityList = MF.AreaManager.GetSelect();
            ViewBag.desList = desList;
            var model = MF.NewsManager.Find(Id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Update(NewsCUD im)
        {
            if (im.file != null && im.file.ContentLength > 0)
            {
                im.thumb = $"/Attachment/{Guid.NewGuid().ToString()}/{im.file.FileName}";
                var combinePath = Server.MapPath($"~{im.thumb }");

                Directory.CreateDirectory(Path.GetDirectoryName(combinePath));

                im.file.SaveAs(combinePath);
            }
           

            MF.NewsManager.Update(im);
            return this.JsonNet(new { succuess = true }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(int Id)
        {
            try
            {
                MF.NewsManager.Delete(Id);
                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [OutputCache(Duration = 60 * 60 * 24 * 2, VaryByParam = "*")]
        public ActionResult Find(int Id)
        {
            try
            {
               var res= MF.NewsManager.Find(Id);
                res.content = HttpUtility.HtmlDecode(res.content);
                return this.JsonNet(new { success = true,data=res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetList(NewsQD im)
        {
            try
            {
               var res= MF.NewsManager.GetListBy(im);
                return this.JsonNet(res.ToExtGrid(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

    }
}